package com.xzq.serializer;

import java.io.*;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/22 12:43
 * @Version 1.0.0
 **/
public class JDKSerializer implements Serializer{
    @Override
    public <T> byte[] serializer(T object) {
        try {
            ByteArrayOutputStream aos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(aos);
            oos.writeObject(object);
            byte[] bytes = aos.toByteArray();
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("jdk序列化失败");
        }
    }

    @Override
    public <T> T derializer(byte[] bytes, Class<T> clazz) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            T t = (T) ois.readObject();
            return t;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("jdk反序列化失败");
        }
    }
}
